package com.douqu.game.core.entity.activity;

import com.bean.core.buffer.ByteBuffer;
import com.douqu.game.core.entity.EntityObject;
import com.douqu.game.core.entity.Player;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.protobuf.SGCommonProto;

/**
 * 阵营玩家排行对象
 * @Author: Bean
 * @Description:
 * @Date: 2018-03-16 14:14
 */
public class RankPlayer extends EntityObject {

    private int serverId;

    /**
     * 这个不用存数据库,加载的时候从数据库去取就行了
     */
    private String serverName = "";

    private String objectIndex = "";

    private String avatar = "";

    private String name = "";

    private int camp;

    private int master;

    private int value;

    private long createTime;

    private long updateTime;

    /**
     * 初始化的时候为false
     * 当统一发放过奖励的时候设置成true
     * 在活动服与主服每次重新连接的时候主服进行一次奖励发放,如果活动服收到回调则设置成true
     * 当战斗结束的时候 主服务器发放奖励，并回调设置成true
     * 如果没有发放成功没有收到回调 则不设置 并且把状态发送给主服务，主服根据true 或 false 判断发放奖励
     * 主服如果有异常 要回滚 回调要声明奖励发放未成功！
     */
    private boolean isReward;

    public RankPlayer()
    {
        this.objectIndex = "";
        this.avatar = "";
        this.name = "";
    }

    /**
     * 此构造方法只用来作indexOf使用
     * @param playerIndex
     */
    public RankPlayer(String playerIndex)
    {
        this.objectIndex = playerIndex;
    }

    public void loadFrom(ByteBuffer buffer)
    {
        this.serverId = buffer.readInt();
        this.objectIndex = buffer.readUTF();
        this.avatar = buffer.readUTF();
        this.name = buffer.readUTF();
        this.camp = buffer.readByte();
        this.master = buffer.readInt();
        this.value = buffer.readInt();
        this.createTime = buffer.readLong();
        this.updateTime = buffer.readLong();

        //+++++++++++++++++++++++++++++++新的
        this.isReward = buffer.readBoolean();
        //+++++++++++++++++++++++++++++++
    }

    public void writeTo(ByteBuffer buffer)
    {
        buffer.writeInt(serverId);
        buffer.writeUTF(objectIndex);
        buffer.writeUTF(avatar);
        buffer.writeUTF(name);
        buffer.writeByte(camp);
        buffer.writeInt(master);
        buffer.writeInt(value);
        buffer.writeLong(createTime);
        buffer.writeLong(updateTime);
        buffer.writeBoolean(isReward);
    }


    public void update(int serverId, MatchBaseInfo playerBase, int value)
    {
        this.serverId = serverId;
        this.objectIndex = playerBase.getObjectIndex();
        this.avatar = playerBase.getAvatar();
        this.name = playerBase.getName();
        this.camp = playerBase.getCamp();
        this.master = playerBase.getMaster();
        this.value = value;
        this.createTime = createTime == 0 ? DataFactory.currentTime : createTime;
        this.updateTime = DataFactory.currentTime;
    }

    public void update(int serverId, Player playerBase, int value)
    {
        this.serverId = serverId;
        this.objectIndex = playerBase.getObjectIndex();
        this.avatar = playerBase.avatar;
        this.name = playerBase.getName();
        this.camp = playerBase.camp;
        this.master = playerBase.master;
        this.value = value;
        this.createTime = createTime == 0 ? DataFactory.currentTime : createTime;
        this.updateTime = DataFactory.currentTime;
    }


    public SGCommonProto.CampRankPlayer.Builder parse()
    {
        SGCommonProto.CampRankPlayer.Builder builder = SGCommonProto.CampRankPlayer.newBuilder();
        builder.setServerId(serverId);
        builder.setServerName(serverName);
        builder.setPlayerIndex(objectIndex);
        builder.setPlayerAvatar(avatar);
        builder.setPlayerName(name);
        builder.setValue(value);
        return builder;
    }

    public void clear()
    {
        serverId = 0;
        serverName = "";
        objectIndex = "";
        avatar = "";
        name = "";
        camp = 0;
        master = 0;
        value = 0;
        createTime = 0;
        updateTime = 0;
        isReward = false;
    }

    public String baseInfo()
    {
        StringBuilder builder = new StringBuilder();
        builder.append(name);
        builder.append(" ");
        builder.append(value);
        builder.append(" (");
        builder.append(serverName);
        builder.append(")");

        return builder.toString();
    }

    public String getServerName() {
        return serverName;
    }

    public void setServerName(String serverName) {
        this.serverName = serverName;
    }

    public String getObjectIndex() {
        return objectIndex;
    }

    public void setObjectIndex(String objectIndex) {
        this.objectIndex = objectIndex;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getServerId() {
        return serverId;
    }

    public void setServerId(int serverId) {
        this.serverId = serverId;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public long getCreateTime() {
        return createTime;
    }

    public void setCreateTime(long createTime) {
        this.createTime = createTime;
    }

    public long getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(long updateTime) {
        this.updateTime = updateTime;
    }

    public int getCamp() {
        return camp;
    }

    public void setCamp(int camp) {
        this.camp = camp;
    }

    public int getMaster() {
        return master;
    }

    public void setMaster(int master) {
        this.master = master;
    }

    public boolean isReward() {
        return isReward;
    }

    public void setReward(boolean isReward) {
        this.isReward = isReward;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof RankPlayer)) return false;

        RankPlayer that = (RankPlayer) o;

        if (!objectIndex.equals(that.objectIndex)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return objectIndex == null ? 0 : objectIndex.hashCode();
    }

    @Override
    public String toString() {
        return "RankPlayer{" +
                "serverId=" + serverId +
                ", serverName='" + serverName + '\'' +
                ", objectIndex='" + objectIndex + '\'' +
                ", avatar='" + avatar + '\'' +
                ", name='" + name + '\'' +
                ", camp=" + camp +
                ", master=" + master +
                ", value=" + value +
                ", createTime=" + createTime +
                ", updateTime=" + updateTime +
                ", isReward=" + isReward +
                '}';
    }
}
